# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgbase=sheldon
pkgname=('sheldon' 'sheldon-docs')
pkgver=0.7.3
pkgrel=1
pkgdesc='A fast and configurable shell plugin manager'
arch=('loong64' 'x86_64')
url='https://sheldon.cli.rs'
license=('Apache' 'MIT')
makedepends=(
  'git'
  'rust'
  'mdbook-linkcheck'
  'curl'
)
options=('!lto')
_commit='492fbeba22ffa1a81946549a04dbe9878dde5155'
source=("git+https://github.com/rossmacarthur/sheldon.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgbase"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgbase"
  find -name Cargo.lock -exec rm -f {} \;
  mkdir -p .cargo
  cat > .cargo/config.toml <<EOF
[source.crates-io]
registry = "https://gitee.com/yetist/crates.io-index"
EOF

  # download dependencies
  cargo fetch --target "`uname -m`-unknown-linux-gnu"
}

build() {
  cd "$pkgbase"

  # binary
  cargo build --release --all-features

  # documentation
  mdbook build docs
}

check() {
  cd "$pkgbase"

  cargo test --frozen --all-features
}

package_sheldon() {
  depends=(
    'glibc'
    'gcc-libs'
    'openssl'
    'zlib'
    'curl'
  )
  optdepends=('sheldon-docs: documentation')

  cd "$pkgbase"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" "target/release/$pkgbase"

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgbase" LICENSE*

  # completions
  install -vDm644 completions/sheldon.bash "$pkgdir/usr/share/bash-completion/completions/$pkgbase"
  install -vDm644 completions/sheldon.zsh "$pkgdir/usr/share/zsh/site-functions/_$pkgbase"
}

package_sheldon-docs() {
  pkgdesc+=' (documentation)'

  cd "$pkgbase"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase" ./*.md

  # html manual
  cp -vr docs/book/html "$pkgdir/usr/share/doc/$pkgbase"
}
